package net.uman.web.manager;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.uman.entity.Resource;
import net.uman.framework.modules.web.struts2.Struts2Utils;
import net.uman.service.ResourceManager;
import net.uman.web.CrudActionSupport;
import net.uman.web.util.FileUploadUtils;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 处理文件上传的Action类
 * 
 * @author lihengjun
 * @version1.0
 */
@Namespace("/manager")
@Result(name="listResource", location="resource-list.ftl")
public class FileuploadAction extends CrudActionSupport<Resource> {
	private static final long serialVersionUID = 1L;
	private static final int BUFFER_SIZE = 16 * 1024;
	@Autowired
	private ResourceManager resourceManager;
	/**
	 * 上传的资源
	 */
	private Resource resource;
	/**
	 * 上传的目的
	 */
	private String target;
	/**
	 * 文件描述
	 */
	private String description;
	/**
	 * 上传的文件
	 */
	private File uploadFile;
	/**
	 * 生成的随机文件名
	 */
	private String filePathName;
	/**
	 * 上传的文件名
	 */
	private String fileName;
	

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	/**
	 * 上传
	 * @return
	 * @throws IOException 
	 * @throws Exception 
	 */
	public void upload() throws IOException{
		
		filePathName = FileUploadUtils.upload(getTarget(), getFileName(), getUploadFile());
		
		if("resource".equals(target)){
			Resource newResource = getResource();
			newResource.setAddUserId(user.getId());
			newResource.setAddUserName(user.getName());
			newResource.setUrl(filePathName);
			newResource.setAddDate(new Date());
			resourceManager.addResource(newResource);
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("flag", true);
		jsonObject.put("msg", "success");
		
		//Struts2Utils.renderJson(jsonObject);
//		return null;
	}
	
	/**
	 * 上传图片
	 * @return
	 * @throws IOException 
	 * @throws Exception 
	 */
	public void imageUpload(){
		try {
			upload();
		} catch (IOException e) {
			e.printStackTrace();
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("flag", true);
		jsonObject.put("photoUrl", filePathName);
		
		Struts2Utils.renderJson("ss");
		//return null;
	}
	/**
	 * 上传
	 * @return
	 * @throws IOException 
	 * @throws Exception 
	 */
	public String resourceUpload(){
		try {
			upload();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "listResource";
	}
	

	public Resource getResource() {
		return resource;
	}

	public void setResource(Resource resource) {
		this.resource = resource;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public File getUploadFile() {
		return uploadFile;
	}

	public void setUploadFile(File uploadFile) {
		this.uploadFile = uploadFile;
	}


	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public Resource getModel() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub

	}
}
